<%@ page import="list.control.Guest" %>
<%@ page import="list.model.Telephone" %>
<%@ page import="list.model.User" %>
<%@ page import="list.control.FileConverter" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--路由--%>
<%
    String opt = request.getParameter("opt");
    String id = request.getParameter("id");
    String tag = request.getParameter("tag");
    if (opt != null) {
        switch (opt) {
            case "to":
                response.sendRedirect("../main.jsp?tag=" + tag);
                break;
            default:
                User user = new User();
                if (!user.init(Guest.getUser(session.getId()))) {
                    response.sendRedirect("../index.jsp?opt=login&val=failed");//初始化失败则返回登录页面
                    break;
                }
                String key = request.getParameter("key");//目标
                String pro = request.getParameter("pro");//属性
                String val = request.getParameter("val");//值
                if (opt.equals("output")) {
                    String file = FileConverter.createVcf(FileConverter.makeVcf(user, Integer.parseInt(tag)));
                    response.sendRedirect(file);
                    response.setContentType("application/x-download");
                    response.addHeader("Content-Disposition","attachment;filename=" + file);
                } else {
                    switch (key) {
                        case "contact":
                            switch (opt) {
                                case "update":
                                    out.print(user.updateContact(Integer.parseInt(id), pro, val));
                                    break;
                                case "updateAll":
                                    String nickname = request.getParameter("nickname");
                                    String birthday = request.getParameter("birthday");
                                    //String head = request.getParameter("head");
                                    String work_place = request.getParameter("work_place");
                                    String postcode = request.getParameter("postcode");
                                    String wechat = request.getParameter("wechat");
                                    String qq = request.getParameter("qq");
                                    String alipay = request.getParameter("alipay");
                                    out.print(user.updateContact(Integer.parseInt(id), nickname, birthday, null, work_place, postcode, wechat, qq, alipay));
                                    response.sendRedirect("../main.jsp?tag=" + tag);
                                    break;
                                case "insert":
                                    out.print(user.insertContact(Integer.parseInt(tag)));
                                    response.sendRedirect("../main.jsp?tag=" + tag);
                                    break;
                                case "delete":
                                    out.print(user.deleteContact(Integer.parseInt(tag), Integer.parseInt(id)));
                                    break;
                            }
                            break;
                        case "tag":
                            switch (opt) {
                                case "update":
                                    out.print(user.updateTag(Integer.parseInt(id), val));
                                    break;
                                case "insert":
                                    out.print(user.insertTag());
                                    response.sendRedirect("../main.jsp?tag=" + tag);
                                    break;
                                case "delete":
                                    out.print(user.deleteTag(Integer.parseInt(id)));
                                    break;
                            }
                            break;
                        case "user":
                            if (opt.equals("update")) {
                                if (pro != null && pro.equals("password")) {
                                    String old = request.getParameter("old");
                                    String now = request.getParameter("now");
                                    out.print(user.updatePassword(old, now));
                                } else {
                                    String email = request.getParameter("email");
                                    String phone = request.getParameter("phone");
                                    String birthday = request.getParameter("birthday");
                                    out.print(user.updateAll(email, birthday, phone));
                                    response.sendRedirect("../main.jsp?tag=" + tag);
                                }
                            }
                            break;
                        case "phone":
                            switch (opt) {
                                case "update":
                                    String phoneU = request.getParameter("phone");
                                    String typeU = request.getParameter("type");
                                    String remarkU = request.getParameter("remark");
                                    out.print(Telephone.update(Integer.parseInt(id), phoneU, typeU, remarkU));
                                    break;

                                case "insert":
                                    String phoneI = request.getParameter("phone");
                                    String typeI = request.getParameter("type");
                                    String remarkI = request.getParameter("remark");
                                    out.print(Telephone.insert(Integer.parseInt(id), phoneI, typeI, remarkI));
                                    response.sendRedirect("../main.jsp?tag=" + tag);
                                    break;
                                case "delete":
                                    out.print(Telephone.delete(Integer.parseInt(id), Integer.parseInt(pro)));
                                    break;
                            }
                    }
                }
        }

    }

%>